"""
check if login or not
"""
import portalclient
from portaldb import DbConnection
from portaltime import Time
from portalshell import PortalShell
nt=portalclient.PortalClient()
@auth.requires_login()
def management():
    userid=auth.user.id
    userCms = db(db.cms.user_id==userid).select(orderby=db.cms.id)
    passwd=auth.user.password
    return locals()

    #return dict()
def create():
    serverList= db(db.instances.is_full=='F').select()
    userid=auth.user.id #get user id from auth
    username=auth.user.email
    email=username
    cmsurl=""
    iddb=db(db.cms.user_id==userid).select(orderby=db.cms.id)
    today=Time()
    now=today.getCurTime()
    if iddb:
        length=len(iddb)-1
        cmsid=iddb[length].id+1
    else: cmsid=1
    if request.vars:
        today=Time()
        now=today.getCurTime()
        """get the cmsname from client"""
        cmsname=request.vars.cmsname
        """create new server"""
        serverList= db(db.instances.is_full=='F').select()
        if serverList:
            for item in serverList:
                if item.status=='ACTIVE':
                    cserver=nt.servers.get(item.id)
                    
                    pass
            if cserver==None:
                temp=nt.servers.create('cms'+ now,4,1)
                while nt.servers.get(temp.id).addresses == {}:
                    print "cho cap phat dia chi"
                    pass 
                cserver=nt.servers.get(temp.id) 
                db.instances.insert(id=cserver.id,instance_name=cserver.name,instance_flavor_id=cserver.flavor['id'],
                                                        instance_image_id=cserver.image['id'],
                                                        instance_private_ip=cserver.addresses['private'][0]['addr'],
                                                       # instance_public_ip=item.addresses['public'][0]['addr'],
                                                        instance_created_time=cserver.created,
                                                        instance_key_name=cserver.key_name,status=cserver.status,is_full='F')     
        else:
            temp=nt.servers.create('cms'+ now,4,1)
            while nt.servers.get(temp.id) !='ACTIVE':#nt.servers.get(temp.id).addresses == {} or nt.servers.get(temp.id).addresses == {u'private': []}:
                print nt.servers.get(temp.id).status
                today.sleep()
                pass 
            cserver=nt.servers.get(temp.id)
            db.instances.insert(id=cserver.id,instance_name=cserver.name,instance_flavor_id=cserver.flavor['id'],
                                                        instance_image_id=cserver.image['id'],
                                                        instance_private_ip=cserver.addresses['private'][0]['addr'],
                                                       # instance_public_ip=item.addresses['public'][0]['addr'],
                                                        instance_created_time=cserver.created,
                                                        instance_key_name=cserver.key_name,status=cserver.status,is_full='F')  
            
        """step 2: create new database from server"""
        
        passwd=str(userid)+'123456'
        cmsdb=DbConnection()
        cmsdbinfo = cmsdb.setupCmsDb(str(userid),passwd,str(cmsid))
        """step 3: ssh to server to install """
        
        shell= PortalShell()
        """ connect with private """
        shell.doConnect(cserver.addresses['private'][0]['addr'])
        """timing when call shell"""
        shell.installNuke('192.168.1.3',cmsdbinfo[2],cmsdbinfo[0],cmsdbinfo[1],str(userid),cmsname,username,email)
        
        cmsurl='http://'+cserver.addresses['private'][0]['addr']+'/'+cmsdbinfo[2]
        db.cms.insert(user_id=userid,instance_id=cserver.id,cms_name=cmsname,cms_url=cmsurl,
                      created_time=now, cms_status='T')
        response.flash='Your CMS has been created. You can login with username:'+username+', password: '+cmsdbinfo[1]
        response.flash+='at: '
        return locals()
        """st"""
    response.flash= None   
    return locals()